package com.luzhikun.myshopping.util;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Rookie Coder
 * @Description
 * @Date
 **/
public class DBCPUtil {

    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /*
     * @Author liu-miss
     * @Description 获取数据库连接
     * @Date 15:49 2021/7/12
     * @Param []
     * @return java.sql.Connection
     **/

    private  Connection getConnection()  {
//        Connection connection=null;
//        String username= "root";
//        String password="140489lu";
//        String url="jdbc:mysql://localhost:3306/yueqian";
//        try {
//            if (connection==null){
//                connection =  DriverManager.getConnection(url,username,password);
//            }
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        }
//        return connection;

        try {
             Context context = new InitialContext();
             DataSource dataSource = (DataSource) context.lookup("java:/comp/env/dbcp/servlet");
            System.out.println("进入连接池");
             return dataSource.getConnection();
        } catch (NamingException | SQLException e) {
            System.out.println("出现异常");
            e.printStackTrace();
        }
        return null;
    }

    /*
     * @Author liu-miss
     * @Description 关闭连接方法
     * @Date 15:53 2021/7/12
     * @Param [connection, pst, rs]
     * @return void
     **/

    private   void closeAll(Connection connection, PreparedStatement pst, ResultSet rs)  {
        try {
            if (rs!=null){
                rs.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        try {
            if (pst!=null){
                pst.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        try {
            if (connection!=null){
                connection.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /*
     * @Author liu-miss
     * @Description 增删改方法
     * @Date 17:09 2021/7/12
     * @Param [sql, params]
     * @return int
     **/

    public int update(String sql, Object...params ){
        Connection connection=null;
        PreparedStatement pst=null;

        try {
            connection = getConnection();
            pst=connection.prepareStatement(sql);

            if (params!=null && params.length>0){
                for (int i = 0; i < params.length; i++) {
//                    System.out.println(params[i]);
                    pst.setObject(i+1,params[i]);
                }
            }
            int i = pst.executeUpdate();
            return i;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
                closeAll(connection,pst,null);
        }
        return -1;

    }

    public  Object  query(String sql,Object...params){
        Connection connection=null;
        PreparedStatement pst=null;
        ResultSet rs=null;
        try {
            connection=getConnection();
            pst = connection.prepareStatement(sql);

            if (params!=null && params.length>0){
                for (int i = 0; i < params.length; i++) {
                    pst.setObject(i+1,params[i]);
                }
            }
            rs = pst.executeQuery();
            if (rs.next()){
                System.out.println("------");
                System.out.println(rs.getObject(1));
                System.out.println(rs.getObject(1).getClass());
                System.out.println("------");
                return  rs.getObject(1);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;


    }
    public <T> List<T> query(String sql,Class<T> c,Object...params){
        Connection connection=null;
        PreparedStatement pst=null;
        ResultSet rs=null;
        List<T> list=new ArrayList<>();

        try {
            connection=getConnection();
            pst = connection.prepareStatement(sql);

            if (params!=null && params.length>0){
                for (int i = 0; i < params.length; i++) {
                    pst.setObject(i+1,params[i]);
                }
            }
           rs = pst.executeQuery();
            // 通过反射封装结果集
            // 获取列数
            ResultSetMetaData metaData = rs.getMetaData();
            int columnCount = metaData.getColumnCount();
            System.out.println("列数====="+columnCount);
           while (rs.next()){
               try {
                  // 创建泛型对象
                  T t = c.getConstructor().newInstance();
                   for (int i = 0; i < columnCount ; i++) {
                       String columnName = metaData.getColumnLabel(i+1);
                       System.out.println("columnName"+columnName);
                       String methodName = "set" + columnName.substring(0,1).toUpperCase()+ columnName.substring(1);
                       System.out.println("methodName"+methodName);
                       Method method = c.getMethod(methodName,String.class);
                       System.out.println("method"+method);
                       method.invoke(t,rs.getObject(columnName));
                   }
                   list.add(t);
               } catch (InstantiationException e) {
                   e.printStackTrace();
               } catch (IllegalAccessException e) {
                   e.printStackTrace();
               } catch (InvocationTargetException e) {
                   e.printStackTrace();
               } catch (NoSuchMethodException e) {
                   e.printStackTrace();
               }
           }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }



}
